{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Transforms\n",
    "\n",
    "Demonstration of the use of transforms and the matrix stack."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Created with matplotlib (https://matplotlib.org/) -->\n",
       "<svg height=\"231.84pt\" version=\"1.1\" viewBox=\"0 0 349.2 231.84\" width=\"349.2pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2020-08-12T16:02:48.247859</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.3.0, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linecap:butt;stroke-linejoin:round;}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 231.84 \n",
       "L 349.2 231.84 \n",
       "L 349.2 0 \n",
       "L 0 0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 325.095237 9.905484 \n",
       "L 327.800721 9.905484 \n",
       "L 327.800721 224.64 \n",
       "L 24.104763 224.64 \n",
       "L 24.104763 221.934516 \n",
       "L 325.095237 221.934516 \n",
       "z\n",
       "\" style=\"opacity:0.3;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_1\">\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 90.949393 53.831209 \n",
       "L 102.970134 90.827246 \n",
       "L 71.499425 67.962438 \n",
       "L 110.39936 67.962438 \n",
       "L 78.928651 90.827246 \n",
       "L 90.949393 53.831209 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 131.851205 57.921391 \n",
       "L 141.467798 87.51822 \n",
       "L 116.291231 69.226373 \n",
       "L 147.411179 69.226373 \n",
       "L 122.234612 87.51822 \n",
       "L 131.851205 57.921391 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 172.753018 61.193536 \n",
       "L 180.446292 84.870999 \n",
       "L 160.305038 70.237522 \n",
       "L 185.200997 70.237522 \n",
       "L 165.059743 84.870999 \n",
       "L 172.753018 61.193536 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 213.65483 63.811252 \n",
       "L 219.809449 82.753222 \n",
       "L 203.696447 71.046441 \n",
       "L 223.613213 71.046441 \n",
       "L 207.500211 82.753222 \n",
       "L 213.65483 63.811252 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 254.556642 65.905424 \n",
       "L 259.480338 81.059001 \n",
       "L 246.589936 71.693576 \n",
       "L 262.523349 71.693576 \n",
       "L 249.632947 81.059001 \n",
       "L 254.556642 65.905424 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 90.949393 135.634834 \n",
       "L 102.970134 172.630871 \n",
       "L 71.499425 149.766063 \n",
       "L 110.39936 149.766063 \n",
       "L 78.928651 172.630871 \n",
       "L 90.949393 135.634834 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 133.277788 135.684652 \n",
       "L 142.688534 173.429093 \n",
       "L 112.889455 148.424696 \n",
       "L 151.694632 151.138219 \n",
       "L 118.705616 171.752044 \n",
       "L 133.277788 135.684652 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 175.599234 135.833861 \n",
       "L 182.354136 174.142819 \n",
       "L 154.371865 147.120653 \n",
       "L 192.893229 152.534478 \n",
       "L 158.546624 170.796891 \n",
       "L 175.599234 135.833861 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 217.906813 136.081736 \n",
       "L 221.972963 174.768573 \n",
       "L 195.943826 145.860287 \n",
       "L 233.993704 153.948039 \n",
       "L 198.456845 169.770068 \n",
       "L 217.906813 136.081736 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 260.193676 136.427068 \n",
       "L 261.551264 175.303306 \n",
       "L 237.602073 144.649739 \n",
       "L 274.995091 155.372014 \n",
       "L 238.441108 168.676576 \n",
       "L 260.193676 136.427068 \n",
       "\" style=\"fill:none;stroke:#0000ff;stroke-opacity:0.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_1\">\n",
       "    <path clip-path=\"url(#p4999d59341)\" d=\"M 21.399279 7.2 \n",
       "L 325.095237 7.2 \n",
       "L 325.095237 221.934516 \n",
       "L 21.399279 221.934516 \n",
       "L 21.399279 7.2 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-width:0.25;\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p4999d59341\">\n",
       "   <rect height=\"217.44\" width=\"334.8\" x=\"7.2\" y=\"7.2\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "import vsketch\n",
    "\n",
    "vsk = vsketch.Vsketch()\n",
    "vsk.size(\"a4\", landscape=True)\n",
    "vsk.scale(\"2cm\")\n",
    "\n",
    "# build a star\n",
    "angles = np.linspace(0, 2 * np.pi, 5, endpoint=False)\n",
    "idx = [0, 2, 4, 1, 3, 0]\n",
    "x = np.cos(angles[idx] - np.pi / 2)\n",
    "y = np.sin(angles[idx] - np.pi / 2)\n",
    "\n",
    "with vsk.pushMatrix():\n",
    "    for i in range(5):\n",
    "        with vsk.pushMatrix():\n",
    "            vsk.scale(0.8 ** i)\n",
    "            vsk.polygon(x, y)\n",
    "\n",
    "        vsk.translate(2, 0)\n",
    "\n",
    "vsk.translate(0, 4)\n",
    "\n",
    "for i in range(5):\n",
    "    with vsk.pushMatrix():\n",
    "        vsk.rotate(i * 4, degrees=True)\n",
    "        vsk.polygon(x, y)\n",
    "\n",
    "    vsk.translate(2, 0)\n",
    "\n",
    "vsk.display()\n",
    "vsk.save(\"transforms.svg\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
